package com.example.myapplication;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.SmsMessage;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by 11250 on 2017/10/13.
 */

public class DNoticeOkReceive extends BroadcastReceiver {


    @Override
    public void onReceive(final Context context, Intent intent) {
        Log.v("sdi","uj");
        try {
            if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
                Bundle bundle = intent.getExtras();
                SmsMessage msg = null;
                if (null != bundle) {
                    Object[] smsObj = (Object[]) bundle.get("pdus");
                    for (Object object : smsObj) {
                        msg = SmsMessage.createFromPdu((byte[]) object);
                        Date date = new Date(msg.getTimestampMillis());//时间
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        String receiveTime = format.format(date);
                        if (!TextUtils.isEmpty(msg.getOriginatingAddress())) {
                            startActivity(context,msg.getOriginatingAddress(),"1");
                        }
                    }
                }
            } else {
                TelephonyManager tm = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);
                PhoneStateListener listener = new PhoneStateListener() {
                    @Override
                    public void onCallStateChanged(int state, String incomingNumber) {
                        super.onCallStateChanged(state, incomingNumber);
                        switch (state) {
                            case TelephonyManager.CALL_STATE_IDLE:

                                if (!TextUtils.isEmpty(incomingNumber)) {
                                    startActivity(context,incomingNumber,"2");
                                }
                                break;
                            case TelephonyManager.CALL_STATE_OFFHOOK:
                                break;
                            case TelephonyManager.CALL_STATE_RINGING:
                                break;
                        }
                    }

                };
                tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);

            }
        } catch (Exception e) {
            Log.v("das","w");
            e.printStackTrace();
        }
    }

    private void startActivity(Context context,String incomingNumber,String flag){
//        Log.v("dsa","11" + flag);
//        Intent intent1 = new Intent(context, CNoticeAdOkActivity.class);
//        intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        intent1.putExtra("number", incomingNumber);
//        intent1.putExtra("flag", flag);
//        context.startActivity(intent1);
    }

}